/*******************************************************************************
 * Copyright 2011, 2012 Chris Banes.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *******************************************************************************/
package mamikeji.liaowa.ui.activity.circle;

import android.net.Uri;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import java.util.ArrayList;
import java.util.List;

import mamikeji.liaowa.R;
import mamikeji.liaowa.ui.activity.BaseActivity;
import mamikeji.liaowa.widget.photoview.HackyViewPager;
import mamikeji.liaowa.widget.photoview.PhotoView;


public class PreviewCircleImgActivity extends BaseActivity {
	List<String> urlList = new ArrayList<>();
	private TextView textView_circle_postion;

	boolean bool = false;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_preview_circle_img);
		getBase();
		//取消状态栏
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

		ViewPager viewPager = (HackyViewPager) findViewById(R.id.viewPager_circle_img);
		textView_circle_postion = (TextView) findViewById(R.id.textView_circle_postion);

		urlList = getIntent().getStringArrayListExtra("urlList");
		int position = getIntent().getIntExtra("position",0);
		bool = getIntent().getBooleanExtra("img_url",false);

		viewPager.setAdapter(new SamplePagerAdapter());

		viewPager.setCurrentItem(position);

		if (urlList.size() == 1){
			textView_circle_postion.setText(1 + " / " + 1);
		}else {
			textView_circle_postion.setText((position+1) + " / " + urlList.size());
		}

		viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
			@Override
			public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
			}

			@Override
			public void onPageSelected(int position) {
				textView_circle_postion.setText((position+1) + " / " + urlList.size());
			}

			@Override
			public void onPageScrollStateChanged(int state) {

			}
		});
	}

	class SamplePagerAdapter extends PagerAdapter {

		@Override
		public int getCount() {
			return urlList.size();
		}

		@Override
		public View instantiateItem(ViewGroup container, int position) {
			PhotoView photoView = new PhotoView(container.getContext());
			photoView.setScaleType(ImageView.ScaleType.FIT_CENTER);
			photoView.setImageURI(Uri.parse(urlList.get(position)));
			Glide.with(PreviewCircleImgActivity.this).load(urlList.get(position)).error(bool ? R.drawable.img_baobaotouxiang : R.drawable.img_huodongliebiao).into(photoView);

			// Now just add PhotoView to ViewPager and return it
			container.addView(photoView, LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);

			return photoView;
		}

		@Override
		public void destroyItem(ViewGroup container, int position, Object object) {
			container.removeView((View) object);
		}

		@Override
		public boolean isViewFromObject(View view, Object object) {
			return view == object;
		}

	}

}
